//
//  LJYByTurnsPictureTableViewCell.m
//  WWLNetease
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "WJYByTurnsPictureTableViewCell.h"

@interface WJYByTurnsPictureTableViewCell () 

@property (nonatomic, retain)NSMutableArray *picArr;
@property (nonatomic, retain)NSMutableArray *titleArr;
@property (nonatomic, retain)NSMutableArray *urlIdArr;

@property (nonatomic, retain)UIImageView *tempImageView;
@property (nonatomic, retain)UILabel *textL;
@property (nonatomic, retain)UIView *blackView;

@end

@implementation WJYByTurnsPictureTableViewCell

- (void)setNewsM:(WJYNewsModel *)newsM{
    _newsM = newsM;
    [self getData];
    [self createViews];
}

#pragma mark- 处理数据
- (void)getData
{
    
    NSArray *array = [NSArray array];
    
    array = self.newsM.ads;
    self.picArr = [NSMutableArray array];
    self.titleArr = [NSMutableArray array];
    self.urlIdArr = [NSMutableArray array];
    
    for (NSDictionary *tempDic in array) {
        
        NSString *url = [tempDic objectForKey:@"imgsrc"];
        NSString *title = [tempDic objectForKey:@"title"];
        NSString *urlId = [tempDic objectForKey:@"url"];
        
        [self.picArr addObject:url];
        [self.titleArr addObject:title];
        [self.urlIdArr addObject:urlId];
    }
    
}

- (void)createViews{
    YYCycleScrollView *cycleScrollView = [[YYCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, 200* SIZE) animationDuration:2.0];
    
    
    NSMutableArray *viewArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.picArr.count; i++) {
        self.tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, 200 * SIZE)];
        
        self.blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, 200 * SIZE)];
        self.blackView.backgroundColor = [UIColor blackColor];
        self.blackView.alpha = 0.3;
        [self.tempImageView addSubview:self.blackView];
        
        // 文字Label
        self.textL = [[UILabel alloc]initWithFrame:CGRectMake(10, 180 * SIZE - 5, (375 - 100) * KPropotionWidth, 20)];
        self.textL.text = self.titleArr[i];
        self.textL.textColor = [UIColor whiteColor];
        self.textL.font = [UIFont systemFontOfSize:15];
        [self.tempImageView addSubview:self.textL];
        
        NSURL *url = [NSURL URLWithString:self.picArr[i]];
        [self.tempImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage.jpg"]];
        
        self.tempImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.tempImageView.clipsToBounds = true;
        [viewArray addObject:self.tempImageView];
        
    }
    [cycleScrollView setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {
        return [viewArray objectAtIndex:pageIndex];
    }];
    [cycleScrollView setTotalPagesCount:^NSInteger{
        return self.picArr.count;
    }];
    [cycleScrollView setTapActionBlock:^(NSInteger(pageIndex)) {
        
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"number" object:[NSString stringWithFormat:@"%ld", (long)pageIndex]];
//        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld", (long)pageIndex] forKey:@"number"];
        
        // 获取当前页面信息
        NSString *url = [self.urlIdArr objectAtIndex:(long)pageIndex];
        NSString *titleStr = [self.titleArr objectAtIndex:(long)pageIndex];
        
        NSMutableArray *array = [NSMutableArray arrayWithObjects:url, titleStr, nil];
        
        // 跳转消息
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushImageDetail" object:array];

        
        [self.numberDele sendNumber:[NSString stringWithFormat:@"%ld", (long)pageIndex] arr:array];
        
    }];
    
    
    [self.contentView addSubview:cycleScrollView];
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
